package ma.ensao.egicom.supplier.client.controller;


import javax.validation.Valid;

import ma.ensao.egicom.core.dto.ProductDto;
import ma.ensao.egicom.supplier.client.model.Product;
import ma.ensao.egicom.supplier.client.validation.ProductValidator;

import org.scub.foundation.framework.base.mapping.util.MapperDozerBean;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class ProductController {
    
    /** Mapper Dozer */
    private MapperDozerBean mapperDozerBean;

    /** Services permettant de gérer des produits */
//    private ProductService productService;

    /** Nom du model */
    private String commandName;
    
    /** URL de la page productFormView */
    private String productFormView;
    
    
    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.setValidator(new ProductValidator());
    }
    
    
    
    
    @RequestMapping(value = "pages/add_product")
    public String getForm(@Valid Product productModel, BindingResult result) {

        if (result.hasErrors()) {
            // s'il y a des erreurs, on reste sur le formulaire
            return productFormView;
        } else {
            // Copie du contenu de productModel vers le DTO contactDto
            ProductDto productDto = mapperDozerBean.map(productModel, ProductDto.class);
            
            System.out.println(productDto.getLabel());
            
//            productService.ajouterOuModifierContact(productDto);
            return productFormView;//"redirect:/";
        }
    }
    
    /**
     * Set the value of productService.
     * @param productService the productService to set
     */
//    public void setProductService(ProductService productService) {
//        this.productService = productService;
//    }

    /**
     * Set the value of commandName.
     * @param commandName the commandName to set
     */
    public void setCommandName(String commandName) {
        this.commandName = commandName;
    }

    /**
     * Set the value of productFormView.
     * @param productFormView the productFormView to set
     */
    public void setProductFormView(String productFormView) {
        this.productFormView = productFormView;
    }
    
    /**
     * Set the value of mapperDozerBean.
     * @param mapperDozerBean the mapperDozerBean to set
     */
    public void setMapperDozerBean(MapperDozerBean mapperDozerBean) {
        this.mapperDozerBean = mapperDozerBean;
    }

}
